package com.ytjj.qmyx.mall.utils;

/**
 * @Description TODO
 * @ClassName Base64URL
 *
 *
 * @Version 1.0
 **/

// 使用旧版本 base64 编解码实现增强兼容性

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

import java.io.IOException;

public class Base64URL {
    public static byte[] base64EncodeUrl(byte[] input) throws IOException {
        byte[] base64 = new BASE64Encoder().encode(input).getBytes();
        for (int i = 0; i < base64.length; ++i) {
            switch (base64[i]) {
                case '+':
                    base64[i] = '*';
                    break;
                case '/':
                    base64[i] = '-';
                    break;
                case '=':
                    base64[i] = '_';
                    break;
                default:
                    break;
            }
            return base64;
        }
        return base64;
    }

        public static byte[] base64DecodeUrl(byte[] input2) throws IOException {
            byte[] dBase64 = input2.clone();
            for (int i = 0; i < dBase64.length; ++i) {
                switch (dBase64[i]) {
                    case '*':
                        dBase64[i] = '+';
                        break;
                    case '-':
                        dBase64[i] = '/';
                        break;
                    case '_':
                        dBase64[i] = '=';
                        break;
                    default:
                        break;
                }
                return new BASE64Decoder().decodeBuffer(dBase64.toString());
            }
            return new BASE64Decoder().decodeBuffer(dBase64.toString());
        }
    }

